package com.feinno.urcs.group.dao;

import com.feinno.database.DataRow;
import com.feinno.database.DataTable;
import com.feinno.database.Database;
import com.feinno.urcs.group.entity.GroupListInfo;
import com.feinno.util.Tuple;
import com.feinno.util.Tuple3;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceImplementation;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by honghao on 8/17/15.
 */
@ServiceImplementation
public class GlobalGroupListDaoImpl implements GlobalGroupListDao {

	@FieldSetter("${GroupListDB}")
	private Database gldb;

	@Override
	public List<GroupListInfo> getGroupList(String mobileNo) throws SQLException {
		String sql = "select * from GroupListInfo where MobileNo = ?";
		DataTable dataTable = gldb.executeTable(sql, mobileNo);
		List<GroupListInfo> list = new ArrayList<>();
		for (DataRow row : dataTable.getRows()) {
			GroupListInfo info = new GroupListInfo();
			info.setGroupName(row.getString("GroupName"));
			info.setIsManager(row.getBoolean("IsAdmin"));
			info.setIsCreator(row.getBoolean("IsCreator"));
			info.setConversionId(row.getString("ConversationId"));
			String groupId = row.getString("GroupId");
			String domain = row.getString("Domain");
			info.setGroupUri("sip:1252000199" + groupId + "@" + domain);
			list.add(info);
		}
		return list;
	}

	@Override
	public void addGroup(String mobileNo, GroupListInfo groupListInfo) throws SQLException {
		String sql = "insert into GroupListInfo(MobileNo, GroupId, GroupName, IsCreator, IsAdmin, ConversationId, Domain) values(?, ? ,? ,? ,? ,? ,?)";
		String groupUri = groupListInfo.getGroupUri();
		String groupId = groupUri.substring(14, groupUri.indexOf("@"));
		String domain = groupUri.substring(groupUri.indexOf("@") + 1);
		gldb.executeNonQuery(sql,
				mobileNo,
				groupId,
				groupListInfo.getGroupName(),
				groupListInfo.isCreator(),
				groupListInfo.isManager(),
				groupListInfo.getConversionId(),
				domain);
	}

	@Override
	public void delGroup(String mobileNo, String groupUri) throws SQLException {
		String sql = "delete from GroupListInfo where MobileNo = ? and GroupId = ?";
		String groupId = groupUri.substring(14, groupUri.indexOf("@"));
		gldb.executeNonQuery(sql, mobileNo, groupId);
	}

	@Override
	public void updateGroupName(String mobileNo, String groupUri, String groupName) throws SQLException {
		String sql = "update GroupListInfo set GroupName = ? where MobileNo = ? and GroupId = ?";
		String groupId = groupUri.substring(14, groupUri.indexOf("@"));
		gldb.executeNonQuery(sql, groupName, mobileNo, groupId);
	}

	@Override
	public void changeGroupAdmin(String mobileNo, String groupUri, boolean isAdmin) throws SQLException {
		String sql = "update GroupListInfo set IsAdmin = ? where MobileNo = ? and GroupId = ?";
		String groupId = groupUri.substring(14, groupUri.indexOf("@"));
		gldb.executeNonQuery(sql, isAdmin, mobileNo, groupId);
	}

	@Override
	public Tuple3<Integer, Integer, Integer> queryGroupCount(String mobileNo) throws SQLException {
		// 创建、加入、管理
		String sql = "select * from GroupListInfo where MobileNo = ?";
		DataTable dataTable = gldb.executeTable(sql, mobileNo);
		int createCount = 0;
		int joinCount = 0;
		int manageCount = 0;
		for (DataRow row : dataTable.getRows()) {
			joinCount++;
			if (row.getBoolean("IsCreator")) {
				createCount++;
			}
			if (row.getBoolean("IsAdmin")) {
				manageCount++;
			}
		}
		Tuple3<Integer, Integer, Integer> combo3 = new Tuple3<>(createCount, joinCount, manageCount);
		return combo3;
	}

	@Override
	public void dismissGroup(String groupUri) throws SQLException {
		String sql = "delete from GroupListInfo where GroupId = ?";
		String groupId = groupUri.substring(14, groupUri.indexOf("@"));
		gldb.executeNonQuery(sql, groupId);
	}
}
